<?php
/**
 * dependences 
 * @see Kanedo_Config_Ini
 * @see Kanedo_Router
 * @see Kanedo_Application_Controller_Exception
 * @see Kanedo_Front_Controller
 */
require_once 'Kanedo/Router.php';
require_once 'Kanedo/Config/Ini.php';
require_once 'Kanedo/Application/Controller/Exception.php';
require_once 'Kanedo/Front/Controller.php';

/**
 * Steuert die Applikation
 *
 */
class Kanedo_Application_Controller
{
	/**
	 * Die Instanz des Application Controllers
	 *
	 * @var Kanedo_Application_Controller
	 * @access protected
	 */
	protected static $_instance = null;
	
	/**
	 * Pfad zur Configurationsdatei
	 *
	 * @var string
	 * @access protected
	 */
	protected $iniFile = null;
	/**
	 * Instanz der Iniklasse mit geladener Configdatei
	 *
	 * @var Kanedo_Config_Ini
	 * @access protected
	 */
	protected $_config = null;
	
	/**
	 * Request Object
	 * 
	 * @var Kanedo_Router_Request
	 * @access protected
	 */
	protected $_request = null;
	/**
	 * Konstruktor; Singletone pattern
	 * @access private
	 * 
	 */
	private function __construct()
	{
	 	
	}
	
	/**
	 * Statische Funktoin zum holen der Instance; Singletone pattern
	 *
	 * @access public
	 * @static 
	 * @return Kanedo_Application_Controller
	 */
	public static function getInstance()
	{
		if (self::$_instance === null)
		{
			self::$_instance = new Kanedo_Application_Controller();
		}
		return self::$_instance;
	}
	
	/**
	 * Läd die Configruationsdatei für den App Controller
	 *
	 * @param String $configFile
	 */
	public function loadConfig($configFile)
	{
		$this->iniFile = $configFile;
		$this->_config = new Kanedo_Config_Ini($this->iniFile);
		Kanedo_Registry::getInstance()->_configFile = $configFile;
	}
	
	/**
	 * Methode zum Ausführen der App
	 *
	 * @return void
	 */
	public function run()
	{
		$request = new Kanedo_Router();
		if ($this->_config->general->baseurl)
		{
			$request->setBaseUrl($this->_config->general->baseurl);
		}
		try 
		{
			$this->_request = $request->parseRequest();
		}catch(Kanedo_Application_Controller_Exception $fileException)
		{
			/**
			 * @todo Request Error
			 */
			echo $fileException;
		}
		
		$frontController = Kanedo_Front_Controller::getInstance();
		$frontController->setRequest($this->_request);
		$frontController->initGroup();
		$frontController->initController();
		$frontController->initAction();
				

	}
}
?>